package com.cws.frame.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cws.enmu.RspCode;
import com.cws.frame.exception.CWSException;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;

/**
 * @title 		[全局异常处理拦截器]
 * @description	[一句话描述]
 * @copyright	Copyright 2013 SHIPING INFO Corporation. All rights reserved.
 * @company		SHIPING INFO.
 * @author		Caspar Du
 * @version		v 1.0
 * @create		2014年5月7日 上午12:22:31
 */
public class CWSExceptionHandler implements Interceptor {

	private static final Logger logger = LoggerFactory.getLogger(CWSExceptionHandler.class);

	@Override
	public void intercept(ActionInvocation ai) {
		try {

			ai.invoke();

		} catch (CWSException e1) {

			if (e1.getRspCode().getCode().equals(RspCode.INVALID_TOKENID.getCode())) {

				logger.info(RspCode.INVALID_TOKENID.getDescription());
			} else {
				logger.error(e1.getRspCode().getDescription(), e1);
			}

			logger.debug("wsuri:" + ai.getViewPath() + ai.getMethodName());
			ai.getController().renderJson(e1.getRspCode().toJson());

		} catch (Exception e2) {
			logger.debug("wsuri:" + ai.getViewPath() + ai.getMethodName());
			logger.error(e2.getMessage(), e2);
			ai.getController().renderJson(RspCode.INNER_ERROR.toJson());
		}
	}

}
